<template>
    <!-- 组件的模板 -->
    <div class="page salary-set">
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label title">基本工资单价</label></div>
                <div class="weui-cell__bd">
                    <input id="input" class="weui-input" type="number" placeholder="请输入" v-model="base_wage_price">
                </div>
                <div class="unit-select">
                    <select v-model="base_wage_unit">
                        <option :value="0">元/天</option>
                        <option :value="1">元/时</option>
                    </select>
                </div>                
            </div>
        </div>
        <p class="tip_1">设置后可根据考勤自动计算工资</p>
        <h2 class="bg-color">其他工资构成项</h2>
        <div class="weui-cells weui-cells_checkbox" v-if="extra_config.length > 0">
            <template v-for="item in extra_config">
                <label class="weui-cell weui-check__label" :for="item.key">
                    <div class="weui-cell__hd">
                        <input type="checkbox" class="weui-check" name="checkbox1" :id="item.key" v-model="item.select" :true-value="1" :false-value="0">
                        <i class="weui-icon-checked"></i>
                    </div>
                    <div class="weui-cell__bd">
                        <p>{{item.name}}</p>
                    </div>
                </label>
            </template>
        </div>
        <p class="tip_1">方便对工资明细项进行核算</p>
    </div>
</template>

<script>
    let preNum = 0
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    // import clientUi from 'gapi/clientUi.js'
    import widget from 'gapi/widget.js'
    // import teamApi from 'clientApi/teamApi.js'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                team_id: 0,
                project_id: 0,
                base_wage_price: '',//基本工资单价数值，精确到分
                base_wage_unit: 0,//基本工资单位 0：元/天 1：元/小时
                extra_config: [],
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                //获取用户信息
                // this.team_id = util.getUrddataString('team_id')
                // this.project_id = util.getUrddataString('project_id')
                cache.getCache('KqCacheMessge', (data) => {
                    // console.log(data)
                    this.team_id = data.team_id
                    this.project_id = data.project_id

                    //读取已设置数据
                    this.getData()
                })

                
            },
            getData(){
                console.log({
                        team_id: this.team_id,
                        project_id: this.project_id
                    })
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/wage/component/get',
                    type:'GET',
                    data:{
                        team_id: this.team_id,
                        project_id: this.project_id
                    },
                    timeout:10000,
                    success:(result) => {
                        console.log(result)
                        commonPage.loadPageStatus('loadSuccess')
                        if(result&&result.errno == 0){
                            this.base_wage_price = result.data.setting_data.base_wage_price == 0 ? '' : result.data.setting_data.base_wage_price/100
                            this.base_wage_unit = result.data.setting_data.base_wage_unit
                            this.extra_config = result.data.setting_data.extra_config
                        }else if(result.errno == 26000){

                        }else{
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            doSubmit(){
                // if(this.base_wage_price === ''){
                //     widget.toast('请输入基本工资')
                //     return
                // }
                //验证输入数字格式 并 保留两位小数
                if(!this.disNumber()){
                    return
                }

                let ajaxData = {
                    team_id: this.team_id,
                    project_id: this.project_id,
                    base_wage_price: this.base_wage_price*100,//基本工资单价数值，精确到分
                    base_wage_unit: this.base_wage_unit,//基本工资单位 0：元/天 1：元/小时
                    extra_config: JSON.stringify(this.extra_config)
                }
                console.log({'入参':ajaxData})
                //保存设置
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/wage/component/set',
                    type:'POST',
                    data:ajaxData,
                    timeout:10000,
                    success:(result) => {
                        console.log(result)
                        commonPage.loadPageStatus('loadSuccess')
                        if(result&&result.errno == 0){
                            //埋点
                            let statisticsLog_data = {
                                event_id: '设置工资单-完成',
                                // event_label: data.event_label,(预留，现在不用传此参数)
                                event_param: {
                                    ca_from: 'done',
                                    project_id: this.project_id,
                                }
                            }
                            // alert(JSON.stringify(statisticsLog_data))
                            KCApiApp.statisticsLog(statisticsLog_data)
                            commonPage.goBack(1)
                        }else{
                            widget.toast(result.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
                
            },
            disNumber(){
                if (isNaN(this.base_wage_price)) {
                    widget.toast('基本工资输入格式错误')
                    return false
                }
                if(this.base_wage_price < 0){
                    widget.toast('最低不少于0元，请重新输入')
                    return
                }
                if(this.base_wage_price > 1000){
                    widget.toast('最高不超过1000元，请重新输入')
                    return false
                }
                // console.log(Number(this.base_wage_price).toFixed(2))
                // if(this.base_wage_price !== ''){
                //     this.base_wage_price = Number(this.base_wage_price).toFixed(2)
                // }
                return true
            },
            watchInput(event){
                console.log(event.keyCode)
                // if(event.keyCode == 229){
                //     this.base_wage_price = 777
                // }
                // if(this.base_wage_price == ''){
                //     this.base_wage_price = preNum
                // }else{
                //     if(this.base_wage_price.indexOf('.') > -1){
                //         this.base_wage_price = this.base_wage_price.substring(0, this.base_wage_price.indexOf('.')+3)
                //         // console.log(this.base_wage_price.substring(0, this.base_wage_price.indexOf('.')+3))
                //     }
                //     preNum = this.base_wage_price
                // }
                // this.base_wage_price = Number(this.base_wage_price)
                // console.log(this.base_wage_price)

                // console.log(this.base_wage_price.replace(/[^1-9]{1}[^0-9]*/g,""))
                // this.base_wage_price = this.base_wage_price.replace(/[^1-9]{1}[^0-9]*/g,"")
            }
        },
        created(){

            this.init();

            commonPage.updateTitleBar({
                // backAction: 'checkChange',    // 点击返回按钮的动作（即回调函数名称）
                // leftDisplay: false,            // 是否显示左按钮
                // leftText: '',                // 左按钮文案
                // leftAction: 'checkChange',    // 点击左按钮的动作（即回调函数名称）
                rightDisplay: true,            // 是否显示右按钮
                rightText: '完成',            // 右按钮文案
                rightAction: 'doSubmit'    // 点击右按钮的文案（即回调函数名称）
            })
            window.doSubmit = this.doSubmit
        },
        ready(){
            let _this = this
            $('#input').on('textInput',function(event){
                console.log(event)
                if(event.data == 0 || event.data == 1 || event.data == 2 || event.data == 3 || event.data == 4 || event.data == 5 || event.data == 6 || event.data == 7 || event.data == 8 || event.data == 9){

                    // console.log(event.data)
                    if(_this.base_wage_price == '' && event.data == 0){
                        event.preventDefault()
                    }
                    if(_this.base_wage_price.toString().indexOf('.') > -1 && _this.base_wage_price.toString().length >= _this.base_wage_price.toString().indexOf('.')+3){
                        event.preventDefault()
                    }

                }else if(event.data == '.'){
                    if(_this.base_wage_price.toString().indexOf('.') > -1 || _this.base_wage_price == ''){
                        event.preventDefault()
                    }
                    // console.log(_this.base_wage_price)
                }else{
                    event.preventDefault()
                }
            })
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/salary.css"></style>


